package com.example;

import Dictionary.DataBaseReader;
import Dictionary.DictionaryUI;
import Dictionary.FrameDisplay;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.lib.uno.helper.Factory;
import com.sun.star.lang.XSingleComponentFactory;
import com.sun.star.registry.XRegistryKey;
import com.sun.star.lib.uno.helper.WeakBase;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import sinhalatransliterartor.TransliterationUI;

/**
 * This is the Main class of the extension. All the functionalities are started
 * through this class.
 * @author SUMEDHA
 */
public final class SinhalaForOO extends WeakBase
        implements com.sun.star.lang.XInitialization,
        com.sun.star.frame.XDispatch,
        com.sun.star.lang.XServiceInfo,
        com.sun.star.frame.XDispatchProvider {

    private final XComponentContext m_xContext;
    private com.sun.star.frame.XFrame m_xFrame;
    private static final String m_implementationName = SinhalaForOO.class.getName();
    private static final String[] m_serviceNames = {
        "com.sun.star.frame.ProtocolHandler"};
    //To readData the dictionary data from the database file
    private DataBaseReader dataBaseReader;

    /**
     *
     * @param context
     */
    public SinhalaForOO(XComponentContext context) {
        m_xContext = context;
    }

    ;

    public static XSingleComponentFactory __getComponentFactory(String sImplementationName) {
        XSingleComponentFactory xFactory = null;

        if (sImplementationName.equals(m_implementationName)) {
            xFactory = Factory.createComponentFactory(SinhalaForOO.class, m_serviceNames);
        }
        return xFactory;
    }

    public static boolean __writeRegistryServiceInfo(XRegistryKey xRegistryKey) {
        return Factory.writeRegistryServiceInfo(m_implementationName,
                m_serviceNames,
                xRegistryKey);
    }

    // com.sun.star.lang.XInitialization:
    public void initialize(Object[] object)
            throws com.sun.star.uno.Exception {
        try {
            if (object.length > 0) {
                m_xFrame = (com.sun.star.frame.XFrame) UnoRuntime.queryInterface(
                        com.sun.star.frame.XFrame.class, object[0]);
            }
            /*  
             * Creating a databaseReader object which has the method to readData
             * the data from the dictionary file, and calling the method
             */
            dataBaseReader = new DataBaseReader(m_xContext);
            dataBaseReader.readData();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(SinhalaForOO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SinhalaForOO.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    // com.sun.star.frame.XDispatch:
    public void dispatch(com.sun.star.util.URL aURL,
            com.sun.star.beans.PropertyValue[] aArguments) {
        if (aURL.Protocol.compareTo("com.example.sinhalaforoo:") == 0) {
            if (aURL.Path.compareTo("Dictionary") == 0) {
                /*
                 * If 'Dictionary' selected, starting the full dictionary
                 */
                DictionaryUI ui = new DictionaryUI(dataBaseReader);
                ui.setVisible(true);

                return;
            }
            if (aURL.Path.compareTo("View") == 0) {
                /*  
                 * If 'Show' is clicked calling the method to show the meaning of the selected word
                 */
                try {
                    FrameDisplay frameDisplay = new FrameDisplay(m_xContext, dataBaseReader);
                    frameDisplay.showMeaning();
                } catch (UnsupportedEncodingException ex) {
                    Logger.getLogger(SinhalaForOO.class.getName()).log(Level.SEVERE, null, ex);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(SinhalaForOO.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(SinhalaForOO.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(SinhalaForOO.class.getName()).log(Level.SEVERE, null, ex);
                }

                return;
            }
            if (aURL.Path.compareTo("Transliterator") == 0) {
                /*
                 * Starting the Transliteration UI, Path of the extension is passed to access the KeyMap Image
                 */
                new TransliterationUI(dataBaseReader.getPath(), m_xContext).setVisible(true);

                return;
            }
            if (aURL.Path.compareTo("Help") == 0) {
                try {
                    /*
                     * Starting the help
                     */
                    new HelpView().view(dataBaseReader.getUrlPath());
                } catch (MalformedURLException ex) {
                    Logger.getLogger(SinhalaForOO.class.getName()).log(Level.SEVERE, null, ex);
                }
                return;

            }
        }
    }

    public void addStatusListener(com.sun.star.frame.XStatusListener xControl,
            com.sun.star.util.URL aURL) {
        // add your own code here
    }

    public void removeStatusListener(com.sun.star.frame.XStatusListener xControl,
            com.sun.star.util.URL aURL) {
        // add your own code here
    }

    // com.sun.star.lang.XServiceInfo:
    public String getImplementationName() {
        return m_implementationName;
    }

    public boolean supportsService(String sService) {
        int len = m_serviceNames.length;

        for (int i = 0; i < len; i++) {
            if (sService.equals(m_serviceNames[i])) {
                return true;
            }
        }
        return false;
    }

    public String[] getSupportedServiceNames() {
        return m_serviceNames;
    }

    // com.sun.star.frame.XDispatchProvider:
    public com.sun.star.frame.XDispatch queryDispatch(com.sun.star.util.URL aURL,
            String sTargetFrameName,
            int iSearchFlags) {
        if (aURL.Protocol.compareTo("com.example.sinhalaforoo:") == 0) {
            if (aURL.Path.compareTo("Dictionary") == 0) {
                return this;
            }
            if (aURL.Path.compareTo("View") == 0) {
                return this;
            }
            if (aURL.Path.compareTo("Transliterator") == 0) {
                return this;
            }
            if (aURL.Path.compareTo("Help") == 0) {
                return this;
            }
        }
        return null;
    }

    // com.sun.star.frame.XDispatchProvider:
    public com.sun.star.frame.XDispatch[] queryDispatches(
            com.sun.star.frame.DispatchDescriptor[] seqDescriptors) {
        int nCount = seqDescriptors.length;
        com.sun.star.frame.XDispatch[] seqDispatcher =
                new com.sun.star.frame.XDispatch[seqDescriptors.length];

        for (int i = 0; i < nCount; ++i) {
            seqDispatcher[i] = queryDispatch(seqDescriptors[i].FeatureURL,
                    seqDescriptors[i].FrameName,
                    seqDescriptors[i].SearchFlags);
        }
        return seqDispatcher;
    }
}
